#include <stdio.h>
#include "mvos/mvos_apis.h"
#include "test_apis.h"

extern mv_s32_t mvrtsp_push_video_stream(mv_s32_t ch, mv_v8_t *data, mv_s32_t data_len, mv_u64_t ts_ms);

static mv_s32_t __video_venc_data_cb(mv_s32_t vchn, mv_s32_t type, mv_u8ptr_t frame_data, mv_s32_t frame_len, mv_s32_t flag) 
{
    // mvrtsp_push_video_stream(vchn, (mv_v8ptr_t)frame_data, frame_len, mvos_time_mono_ms(NULL));

    // MVPC_LOG_INF("[VIDEO] vchn:%d type:%d size:%d \n", vchn, type, frame_len);
    return MV_EC_SUCCESS;
}

mv_s32_t test_video_start()
{
    MVP_VI_ATTR_T       _st_vi_attr         = {0};
    MVP_VENC_ATTR_T     _ast_venc_attr[3]   = {0};
    MVP_SENSOR_INFO_T   _stru_sensor_info   = {0};
    mvp_isp_sensor_info_get(&_stru_sensor_info);

    _st_vi_attr.vi_info[MV_VIDEO_MAIN_CHN].width    = _stru_sensor_info.sensor_chn[0].sensor_width;
    _st_vi_attr.vi_info[MV_VIDEO_MAIN_CHN].height   = _stru_sensor_info.sensor_chn[0].sensor_height;
    _st_vi_attr.vi_info[MV_VIDEO_SUB_CHN].width     = 640;
    _st_vi_attr.vi_info[MV_VIDEO_SUB_CHN].height    = 360;
    _st_vi_attr.vi_info[MV_VIDEO_MJPEG_CHN].width   = 640;
    _st_vi_attr.vi_info[MV_VIDEO_MJPEG_CHN].height  = 360;

    memcpy(&_st_vi_attr.sensor_info, &_stru_sensor_info, sizeof(MVP_SENSOR_INFO_T));
    _st_vi_attr.vi_crop.width                       = _stru_sensor_info.sensor_chn[0].sensor_width;
    _st_vi_attr.vi_crop.height                      = _stru_sensor_info.sensor_chn[0].sensor_height;
    _st_vi_attr.vi_crop.left                        = 0;
    _st_vi_attr.vi_crop.top                         = 0;
    _st_vi_attr.lens_type                           = 0;

    _ast_venc_attr[0].venc_res.width                = _stru_sensor_info.sensor_chn[0].sensor_width;
    _ast_venc_attr[0].venc_res.height               = _stru_sensor_info.sensor_chn[0].sensor_height*_stru_sensor_info.sensor_num;
    _ast_venc_attr[0].bit_rate_attr.fps             = 15;
    _ast_venc_attr[0].bit_rate_attr.goplen          = 30;
    _ast_venc_attr[0].bit_rate_attr.target_kbps     = 1536;
    _ast_venc_attr[0].bit_rate_attr.max_kbps        = 2000;
    _ast_venc_attr[0].bit_rate_attr.minqp           = 20;
    _ast_venc_attr[0].bit_rate_attr.maxqp           = 48;
    _ast_venc_attr[0].br_mode                       = MV_BR_MODE_VBR;
    _ast_venc_attr[0].encode_type                   = MV_HEVC_ENC_TYPE;
    _ast_venc_attr[0].smart_mode                    = MV_SMART_MODE_DISABLE;

    _ast_venc_attr[1].venc_res.width                = 640;
    _ast_venc_attr[1].venc_res.height               = 360*_stru_sensor_info.sensor_num;
    _ast_venc_attr[1].bit_rate_attr.fps             = 15;
    _ast_venc_attr[1].bit_rate_attr.goplen          = 30;
    _ast_venc_attr[1].bit_rate_attr.target_kbps     = 1024;
    _ast_venc_attr[1].bit_rate_attr.max_kbps        = 1024;
    _ast_venc_attr[1].bit_rate_attr.minqp           = 20;
    _ast_venc_attr[1].bit_rate_attr.maxqp           = 48;
    _ast_venc_attr[1].br_mode                       = MV_BR_MODE_VBR;
    _ast_venc_attr[1].encode_type                   = MV_HEVC_ENC_TYPE;
    _ast_venc_attr[1].smart_mode                    = MV_SMART_MODE_DISABLE;

    _ast_venc_attr[2].venc_res.width                = 640;
    _ast_venc_attr[2].venc_res.height               = 360*_stru_sensor_info.sensor_num;
    _ast_venc_attr[2].bit_rate_attr.fps             = 15;
    _ast_venc_attr[2].bit_rate_attr.goplen          = 15*3;
    _ast_venc_attr[2].bit_rate_attr.target_kbps     = 1024;
    _ast_venc_attr[2].bit_rate_attr.max_kbps        = 1024;
    _ast_venc_attr[2].bit_rate_attr.minqp           = 20;
    _ast_venc_attr[2].bit_rate_attr.maxqp           = 48;
    _ast_venc_attr[2].br_mode                       = MV_BR_MODE_AVBR;
    _ast_venc_attr[2].encode_type                   = MV_MJPEG_ENC_TYPE;
    _ast_venc_attr[2].smart_mode                    = MV_SMART_MODE_DISABLE;
    _ast_venc_attr[2].jpeg_enc_type                 = MV_HARD_ENCODE;

    mvp_isp_init(&_st_vi_attr);

    // video
    mvp_video_vi_init(&_st_vi_attr);
    mvp_video_venc_init(MV_VIDEO_MAIN_CHN , &_ast_venc_attr[MV_VIDEO_MAIN_CHN]);
    mvp_video_venc_init(MV_VIDEO_SUB_CHN  , &_ast_venc_attr[MV_VIDEO_SUB_CHN]);
    mvp_video_venc_init(MV_VIDEO_MJPEG_CHN, &_ast_venc_attr[MV_VIDEO_MJPEG_CHN]);
    mvp_video_venc_chn_enable(MV_VIDEO_MAIN_CHN);
    mvp_video_venc_chn_enable(MV_VIDEO_SUB_CHN);
    mvp_video_venc_start(NULL, __video_venc_data_cb);

    return MV_EC_SUCCESS;
}


mv_s32_t test_video_stop()
{
    mvp_video_venc_chn_disable(MV_VIDEO_MAIN_CHN);
    mvp_video_venc_chn_disable(MV_VIDEO_SUB_CHN);
    mvp_video_venc_stop();
    mvp_video_venc_uninit(MV_VIDEO_MAIN_CHN);
    mvp_video_venc_uninit(MV_VIDEO_SUB_CHN);
    mvp_video_venc_uninit(MV_VIDEO_MJPEG_CHN);
    mvp_video_vi_uninit();
    mvp_isp_uninit();
    return MV_EC_SUCCESS;
}